package com.dkm.orproject.dkmoa.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.OaLog;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaSupplier;
import com.dkm.orproject.dkmoa.service.OaSupplierService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *  @ClassName : OaSupplierAction  //供应商控制层类名
 *  @Description : 供应商控制层
 *  @Author : zmh //作者
 *  @Date: 2020-11-25 09:28  //时间
 */
@RestController
@RequestMapping("/OaSupplierAction")
public class OaSupplierAction {
    /**
     * 工具类
     */
    @Resource
    Tools tools;
    /**
     * 供应商服务接口
     */
    @Resource
    private OaSupplierService oaSupplierService;

    /**
     * 通过id查询供应商supplier信息
     * @param pkSupplierId 该供应商supplierT的id值
     * @param request  服务器请求
     * @return 返回应商对象supplier存入map中
     */
    @GetMapping("findById/{pkSupplierId}")
    @UserLoginToken
    public Map<String, Object> selectByPrimaryKey(@PathVariable("pkSupplierId") Long pkSupplierId, HttpServletRequest request) {
        OaPersonnel oaPersonnel = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaSupplier supplier = oaSupplierService.selectByPrimaryKey(pkSupplierId);
            OaLog log = new OaLog();
            log.setLogTitle("供应商id查询模块查询成功");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(oaPersonnel).toString());
            tools.rowMap(map, 1, null);
            map.put("data", supplier);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("供应商根据id查询模块异常");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
        }
        return map;
    }

    /**
     * 根据id删除一条供应商信息
     * @param pkSupplierId 供应商的id值
     * @param session 会话
     * @param request 服务器请求
     * @return 回调函数
     */
    @UserLoginToken
    @DeleteMapping("deleteSupplierById/{pkSupplierId}")
    public Map<String, Object> deleteByPrimaryKey(@PathVariable("pkSupplierId") Long pkSupplierId, HttpSession session, HttpServletRequest request) {
        OaPersonnel oaPersonnel = (OaPersonnel) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaLog log = new OaLog();
            OaSupplier supplier = new OaSupplier();
            supplier.setPkSupplierId(pkSupplierId);
            supplier.setIsRevision("0");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogTitle("供应商的删除模块");
            int rows = oaSupplierService.deleteByPrimaryKey(pkSupplierId);
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            log.setLogBody(JSON.toJSON(oaPersonnel).toString());
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("供应商的删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            tools.errorMap(map, log);
            return map;
        }


    }

    /**
     * 分页查询所有供应商的信息
     * @param oaSupplier 供应商模型
     * @param page  当前页数
     * @param limit  显示的行数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param request 请求对象
     * @return
     */
    @UserLoginToken
    @GetMapping("selectAll")
    public Map<String,Object> findAll(OaSupplier oaSupplier, Integer page, Integer limit, String startTime, String endTime, HttpServletRequest request){
        try {
            //查询总行数
            Long rowCount=oaSupplierService.rowCount(oaSupplier,startTime,endTime);
            //分页查询集合
            List<OaSupplier> logs=oaSupplierService.selectAll(oaSupplier,startTime,endTime,page,limit);
            //返回集合
            return Tools.maps(logs,rowCount);
        } catch (Exception e) {
            e.printStackTrace();
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("查询供应商的所有方法");
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 批量删除供应商
     * @param ids 删除的条数的id数组
     * @param session 从session中获得user对象
     * @param request 请求对象
     * @return
     */
    @DeleteMapping("deleteAll")
    @UserLoginToken
    public Map<String, Object> deleteAll(@RequestParam(value = "ids[]", required = false) Long[] ids, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        List<Long> list;
        Map<String,Object> map=new HashMap<>();
        if (ObjectUtils.isEmpty(ids)){
            return tools.rowMap(map,0,null);
        }else {
            list= Arrays.asList(ids);
        }
        try {
            OaLog log = new OaLog();
            int deleteAll = oaSupplierService.deleteAll(list);
            log.setLogType("1");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogTitle("供应商类型批量删除模块");
            log.setCreatedBy(user.getPersonnelName());
            tools.rowMap(map, deleteAll, log);
            map.put("data",deleteAll);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogBody(JSON.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("供应商删除模块异常");
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 增加供应商和修改规则制度类型
     * @param oaSupplier 供应商对象
     * @param request  请求对象
     * @return map 中存入了增加和修改成功的条数
     */
    @PostMapping("updateOrAdd")
    @UserLoginToken
    public Map<String, Object> add(OaSupplier oaSupplier, HttpServletRequest request) {
        OaPersonnel oaPersonnel = (OaPersonnel) request.getSession().getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtils.isEmpty(oaPersonnel)) {
            tools.rowMap(map, 0, null);
        }
        try {
            int rows = 0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if (oaSupplier.getPkSupplierId() != null && !"".equals(oaSupplier.getPkSupplierId())) {
                log.setLogTitle("供应商修改模块");
                oaSupplier.setCreatedBy(oaPersonnel.getPersonnelName());
                rows = oaSupplierService.updateByPrimaryKeySelective(oaSupplier);
            } else {
                oaSupplier.setCreatedBy(oaPersonnel.getPersonnelName());
                oaSupplier.setIsRevision("1");
                log.setLogTitle("供应商增加模块");
                oaSupplier.setCreatedBy(oaPersonnel.getPersonnelName());
                rows = oaSupplierService.insertSelective(oaSupplier);
            }
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(oaPersonnel).toString());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogTitle("供应商增加或修改模块异常");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map, log);
            return map;
        }
    }
    /**
     * 日期格式化
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // true:允许输入空值，false:不能为空值
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}
